package net.swl.linkedvillage;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;

import com.parse.LogInCallback;
import com.parse.ParseException;
import com.parse.ParseUser;
//e

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		getActionBar().setIcon(R.drawable.logo_linkedvillage);
		setTitle("");
		/*Parse.initialize(this, "VfKri7peCV168zVhdWZBZ8zwZmjZBqWs1GL7Hcoi","0iuOEQKfh6dVM6Y8UYwjAXphYtPchnyEO12jxCyy");

		ParseUser currentUser = ParseUser.getCurrentUser();
		if (currentUser != null) {
			Intent intent = new Intent(this, IndexActivity.class);
			startActivity(intent);
		}
		*/
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	public void onRegisterClick(View v) {
		Intent i = new Intent(this, RegistarActivity.class);
		startActivity(i);
	}

	public void onLoginClick(View v) {
		/*
		EditText txt = (EditText) findViewById(R.id.loginUserName);
		String username = txt.getEditableText().toString();
		txt = (EditText) findViewById(R.id.loginPassword);
		String pass = txt.getEditableText().toString();
		login(username, pass);
		*/
		Intent intent = new Intent(this, IndexActivity.class);
		startActivity(intent);
	}

	private void login(String username, String password) {
		ParseUser.logInInBackground(username, password, new LogInCallback() {
			@Override
			public void done(ParseUser user, ParseException ex) {
				if (ex == null) {
					onUserLoggedIn(user);
				} else {
					onLoginFailed(ex);
				}
			}
		});
	}

	private void onUserLoggedIn(ParseUser user) {
		Log.i("Login", "Login com sucesso: " + user.getUsername());
		Intent i = new Intent(this, IndexActivity.class);
		startActivity(i);
	}

	private void onLoginFailed(ParseException ex) {
		Log.e("Login", "Erro no login: " + ex.getMessage());

	}

}
